/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

public class Get_Data_UrlCon
{
	private HttpURLConnection	conn		= null;
	private URL					realUrl		= null;
	public String				Error_Msg	= "";
	public String				strResult	= "";

	public void Open_Conn(String strUrl, String Get_Post)
	{
		try
		{
			realUrl = new URL(strUrl);
			conn = (HttpURLConnection) realUrl.openConnection();
			conn.setRequestProperty("accept","*/*");
			conn.setRequestProperty("connection","Keep-Alive");
			conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			conn.setRequestProperty("content-type","application/x-www-form-urlencoded;charset=UTF-8");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod(Get_Post);
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString(),"网络或服务异常");
			Error_Msg = "Open_Conn错误:" + e.getMessage();
		}
	}

	public String getPostData(String InputPostData)
	{
		OutputStream out = null;
		BufferedReader in = null;
		try
		{
			out = conn.getOutputStream();
			out.write(InputPostData.getBytes(Com_Para.U_CODE));
			out.flush();
			int resultCode = conn.getResponseCode();
			if(HttpURLConnection.HTTP_OK == resultCode)
			{
				in = new BufferedReader(new InputStreamReader(conn.getInputStream(),Com_Para.U_CODE));
				String line;
				while(!java.util.Objects.equals((line = in.readLine()),null))
				{
					strResult += line;
				}
			}
		}
		catch(Exception e)
		{
			Error_Msg = "getPostData错误:" + e.getMessage();
			strResult = e.getMessage();
			Com_Fun.debug_in(e,this.toString(),"getPostData错误");
		}
		finally
		{
			try
			{
				if(!java.util.Objects.equals(out,null))
				{
					out.close();
				}
				if(!java.util.Objects.equals(in,null))
				{
					in.close();
				}
				conn.disconnect();
			}
			catch(IOException ex)
			{
			}
		}
		return strResult;
	}
}